﻿//  --------------------------------
//  Copyright (c) AW Systems, Inc. All rights reserved.
//  This source code is made available under the terms of the Microsoft Public License (Ms-PL)
//  http://www.codeplex.com/SLAM/license
//  ---------------------------------

using System;
using System.Web;
using AWS.SLAM.Activation;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace AWS.SLAM.Administration
{
    public partial class Activation : System.Web.UI.Page
    {
        private string slamFolder = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            //if under a site collection other than the root, doing a simple
            //redirect to the page will always redirect to the root, so the desired
            //location needs extracted from the RawUrl
            slamFolder = Request.RawUrl.Substring(0, Request.RawUrl.LastIndexOf("/") + 1);

            if (!IsPostBack)
            {
                //Test if the 3.5 Framework is installed
                if(!InstallationValidator.VerifyNetFx35IsInstalled())
                {
                    Response.Redirect(slamFolder + "SLAMControlPanel.aspx");
                } 
                else 
                {
                    //Test if SLAM can clear instances, if it cannot go back to the Control Panel
                    try
                    {
                        ComponentService.ClearInstances();
                    }
                    catch (Exception ex)
                    {
                        Response.Redirect(slamFolder + "SLAMControlPanel.aspx");
                    }
                }
            }
            else
            {
                ComponentService.ClearInstances();
            }
        }

        /// <summary>
        /// Event handler for synchronizing SLAM
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSynchronize_Click(object sender, EventArgs e)
        {
            Response.Redirect(slamFolder + "Synchronization.aspx");
        }

        /// <summary>
        /// Event handler for reactivating SLAM
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAttachEvents_Click(object sender, EventArgs e)
        {
            IActivator activator = ComponentService.GetInstance<IActivator>();
            SPWeb currentSite = SPControl.GetContextWeb(HttpContext.Current);
            if (currentSite.Properties["SLAMLastActivationDate"] != null)
            {
                activator.Deactivate();
                currentSite.Properties["SLAMLastActivationDate"] = DateTime.Now.ToString();
                currentSite.Properties.Update();
            }
            else
            {
                currentSite.Properties.Add("SLAMLastActivationDate", DateTime.Now.ToString());
                currentSite.Properties.Update();
            }

            activator.Activate(currentSite.Site.ServerRelativeUrl.TrimStart('/'));
            ComponentService.ClearInstances();
            Response.Redirect(slamFolder + "Diagnostics.aspx");
        }

        /// <summary>
        /// Event handler for deactivating SLAM
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDetachEvents_Click(object sender, EventArgs e)
        {
            IActivator activator = ComponentService.GetInstance<IActivator>();
            activator.Deactivate();
            ComponentService.ClearInstances();
            Response.Redirect(slamFolder + "Diagnostics.aspx");
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect(slamFolder + "SLAMControlPanel.aspx");
        }
    }
}
